package anims.circle_bounce_;
/**

 *@author Christopher C Whiting
 * 03/26/07
 * Lab 5
 * Version 1.0
 * 
 * Runs the bounce program. Creates two circles which move around on the screen.
 * 	Circles change direction if they touch the edge or each other.
 */

import java.awt.Color;
import java.awt.Frame;

public class Lab5 extends Frame{
	// INSTANCE VARIABLES
    private Circle _circle1, _circle2; // variables for each circle
	   
    // CONSTRUCTOR - creates circle1 and circle 2
    public Lab5 () {
		
		// circle 1
		_circle1 = new Circle(100,100,25,5,8);
		_circle1.setColor(Color.BLUE);
		_circle1.setFrameColor(Color.BLACK);
		
		// circle 2
		_circle2 = new Circle(200,200,25,10,5);
		_circle2.setColor(Color.GREEN);
		_circle2.setFrameColor(Color.BLACK);
		
	 	while(true){
		    //pause for 100 ms
		    try {Thread.sleep(100);} catch (Exception e) {}
		   
			_circle1.wallBounce(_circle1);
			_circle1.move();
			
		    _circle2.wallBounce(_circle2);
			_circle2.move();
			
			this.collision(_circle1, _circle2);
			// System.out.println("Lab5 while Loop");  // WHILE LOOP WORKS AS IS
		}
	}
	
	// calculates distance
	public double distance(int x1, int y1, int x2, int y2) {
		return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
	}
	
	// checks for a collision using distance method
	public void collision(Circle one, Circle two){
		int cx1 = _circle1.getXCenter();
		int cy1 = _circle1.getYCenter();
		int r1 = _circle1.getRadius();
		
		int cx2 = _circle2.getXCenter();
		int cy2 = _circle2.getXCenter();
		int r2 = _circle2.getRadius();
		double distance = distance(cx1,cy1,cx2,cy2);
		System.out.println("Distance = " + distance);
		
		if (distance <= r1 + r2){
			_circle1.setVelocity();
			_circle2.setVelocity();
			System.out.println("COLLISION");
		}
	}
    
	// MAIN METHOD
	public static void main (String[]args){
		Lab5 app = new Lab5();
	}
}
